﻿using System;
using System.Configuration;
using Daneas.Utility.Universals;

namespace Daneas.Utility.Data
{

    public class PubConstant
    {
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                if (ConfigurationManager.AppSettings["connName"] == null)
                {
                    return "-1";
                }

                string connName = ConfigurationManager.AppSettings["connName"].ToString();

                if (string.IsNullOrEmpty(connName))
                {
                    try
                    {
                        object obj = DataCache.GetCache("dsic");//dsic要加密

                        if (obj == null || obj == DBNull.Value)
                        {
                            return "0";
                        }

                        string xml = (string)obj;

                        string[] l = xml.Split(',');

                        string ser = l[0];
                        ser = ser.Replace("a", ",");
                        ser = ser.Replace("l", "0");
                        ser = ser.Replace("c", "1");
                        ser = ser.Replace("m", "2");
                        ser = ser.Replace("e", "3");
                        ser = ser.Replace("n", "4");
                        ser = ser.Replace("g", "5");
                        ser = ser.Replace("s", "6");
                        ser = ser.Replace("x", "7");
                        ser = ser.Replace("z", "8");
                        ser = ser.Replace("k", "9");

                        string to = l[1].Replace("dsv_", "");
                        string id = l[2].Replace("app_", "");
                        string pw = l[3].Replace("app_", "");

                        return "Data Source=" + ser + ";Initial Catalog=" + to + ";Persist Security Info=True;User ID=" + id + ";Password=" + pw;
                    }
                    catch
                    {
                        return "00";
                    }
                }
                else
                {
                    try
                    {
                        string _connectionString = ConfigurationManager.ConnectionStrings[connName].ToString();

                        if (string.IsNullOrEmpty(_connectionString))
                        {
                            _connectionString = ConfigurationManager.AppSettings[connName].ToString();
                        }

                        //加密
                        //string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
                        //if (ConStringEncrypt == "true")
                        //{
                        //    _connectionString = DESEncrypt.Decrypt(_connectionString);
                        //}

                        return _connectionString;
                    }
                    catch
                    {
                        return "1";
                    }
                }

            }
        }

        public static int TimeOut
        {
            get { return 10; }
        }

        /// <summary>
        /// 得到web.config里配置项的数据库连接字符串。
        /// </summary>
        /// <param name="configName"></param>
        /// <returns></returns>
        public static string GetConnectionString(string configName)
        {
            string connectionString = ConfigurationManager.AppSettings[configName];
            string ConStringEncrypt = ConfigurationManager.AppSettings["ConStringEncrypt"];
            if (ConStringEncrypt == "true")
            {
                connectionString = DESEncrypt.Decrypt(connectionString);
            }
            return connectionString;
        }


    }
}
